home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / mesa-amiwin / src-aux / glaux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-03  |  9.3 KB  |  441 lines

  1. /* aux.c */
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #if !defined(AMIGA) && !defined(__WIN32__)
  8. #include <X11/Xlib.h>
  9. #include <X11/Xutil.h>
  10. #include <X11/keysym.h>
  11. #endif
  12. #include <GL/gl.h>
  13. #include "gltk.h"
  14. #include "glaux.h"
  15.  
  16. #ifdef AMIWIN
  17. #include <pragmas/xlib_pragmas.h>
  18. extern struct Library *XLibBase;
  19. #endif
  20.  
  21. #if defined(__cplusplus) || defined(c_plusplus)
  22. #define class c_class
  23. #endif
  24.  
  25.  
  26. static struct {
  27.     int keyField;
  28.     void (*KeyFunc)(void);
  29. } keyTable[200];
  30.  
  31. static struct {
  32.     int mouseField;
  33.     void (*MouseFunc)(AUX_EVENTREC *);
  34. } mouseDownTable[20], mouseUpTable[20], mouseLocTable[20];
  35.  
  36. static int keyTableCount = 0;
  37. static int mouseDownTableCount = 0;
  38. static int mouseUpTableCount = 0;
  39. static int mouseLocTableCount = 0;
  40. static GLenum displayModeType = 0;
  41. static GLenum displayModePolicy = AUX_MINIMUM_CRITERIA;
  42. static int displayModeID = 0;
  43.  
  44. static int animate = 0;
  45.  
  46.  
  47. #ifdef __WIN32__
  48. #define NCOLORS 17
  49. float auxRGBMap[NCOLORS][3] = {
  50.     {0,0,0},
  51.     {0,0,0},
  52.     {0,0,0},
  53.     {0,0,0},
  54.     {0,0,0},
  55.     {0,0,0},
  56.     {0,0,0},
  57.     {0,0,0},
  58.     {0,0,0},
  59.     {0,0,0},
  60.     {1,0,0},
  61.     {0,1,0},
  62.     {1,1,0},
  63.     {0,0,1},
  64.     {1,0,1},
  65.     {0,1,1},
  66.     {1,1,1}
  67. };
  68. #endif
  69.  
  70.  
  71. static void DefaultHandleReshape(int w, int h)
  72. {
  73.     glViewport(0, 0, w, h);
  74.     glMatrixMode(GL_PROJECTION);
  75.     glLoadIdentity();
  76.     glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
  77.     glMatrixMode(GL_MODELVIEW);
  78.     glLoadIdentity();
  79. }
  80.  
  81. static void DefaultHandleExpose(int w, int h)
  82. {
  83. }
  84.  
  85. static GLenum MouseLoc(int x, int y, GLenum button)
  86. {
  87.     AUX_EVENTREC info;
  88.     GLenum flag;
  89.     int i;
  90.  
  91.     flag = GL_FALSE;
  92.     for (i = 0; i < mouseLocTableCount; i++) {
  93.     if ((button & AUX_LEFTBUTTON) == mouseLocTable[i].mouseField) {
  94.         info.event = AUX_MOUSELOC;
  95.         info.data[AUX_MOUSEX] = x;
  96.         info.data[AUX_MOUSEY] = y;
  97.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  98.         (*mouseLocTable[i].MouseFunc)(&info);
  99.         flag |= GL_TRUE;
  100.     }
  101.     if ((button & AUX_RIGHTBUTTON) == mouseLocTable[i].mouseField) {
  102.         info.event = AUX_MOUSELOC;
  103.         info.data[AUX_MOUSEX] = x;
  104.         info.data[AUX_MOUSEY] = y;
  105.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  106.         (*mouseLocTable[i].MouseFunc)(&info);
  107.         flag |= GL_TRUE;
  108.     }
  109.     if ((button & AUX_MIDDLEBUTTON) == mouseLocTable[i].mouseField) {
  110.         info.event = AUX_MOUSELOC;
  111.         info.data[AUX_MOUSEX] = x;
  112.         info.data[AUX_MOUSEY] = y;
  113.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  114.         (*mouseLocTable[i].MouseFunc)(&info);
  115.         flag |= GL_TRUE;
  116.     }
  117.     }
  118.     return flag;
  119. }
  120.  
  121. static GLenum MouseUp(int x, int y, GLenum button)
  122. {
  123.     AUX_EVENTREC info;
  124.     GLenum flag;
  125.     int i;
  126.  
  127.     flag = GL_FALSE;
  128.     for (i = 0; i < mouseUpTableCount; i++) {
  129.     if ((button & AUX_LEFTBUTTON) == mouseUpTable[i].mouseField) {
  130.         info.event = AUX_MOUSEUP;
  131.         info.data[AUX_MOUSEX] = x;
  132.         info.data[AUX_MOUSEY] = y;
  133.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  134.         (*mouseUpTable[i].MouseFunc)(&info);
  135.         flag |= GL_TRUE;
  136.     }
  137.     if ((button & AUX_RIGHTBUTTON) == mouseUpTable[i].mouseField) {
  138.         info.event = AUX_MOUSEUP;
  139.         info.data[AUX_MOUSEX] = x;
  140.         info.data[AUX_MOUSEY] = y;
  141.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  142.         (*mouseUpTable[i].MouseFunc)(&info);
  143.         flag |= GL_TRUE;
  144.     }
  145.     if ((button & AUX_MIDDLEBUTTON) == mouseUpTable[i].mouseField) {
  146.         info.event = AUX_MOUSEUP;
  147.         info.data[AUX_MOUSEX] = x;
  148.         info.data[AUX_MOUSEY] = y;
  149.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  150.         (*mouseUpTable[i].MouseFunc)(&info);
  151.         flag |= GL_TRUE;
  152.     }
  153.     }
  154.     return flag;
  155. }
  156.  
  157. static GLenum MouseDown(int x, int y, GLenum button)
  158. {
  159.     AUX_EVENTREC info;
  160.     GLenum flag;
  161.     int i;
  162.  
  163.     flag = GL_FALSE;
  164.     for (i = 0; i < mouseDownTableCount; i++) {
  165.     if ((button & AUX_LEFTBUTTON) == mouseDownTable[i].mouseField) {
  166.         info.event = AUX_MOUSEDOWN;
  167.         info.data[AUX_MOUSEX] = x;
  168.         info.data[AUX_MOUSEY] = y;
  169.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  170.         (*mouseDownTable[i].MouseFunc)(&info);
  171.         flag |= GL_TRUE;
  172.     }
  173.     if ((button & AUX_RIGHTBUTTON) == mouseDownTable[i].mouseField) {
  174.         info.event = AUX_MOUSEDOWN;
  175.         info.data[AUX_MOUSEX] = x;
  176.         info.data[AUX_MOUSEY] = y;
  177.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  178.         (*mouseDownTable[i].MouseFunc)(&info);
  179.         flag |= GL_TRUE;
  180.     }
  181.     if ((button & AUX_MIDDLEBUTTON) == mouseDownTable[i].mouseField) {
  182.         info.event = AUX_MOUSEDOWN;
  183.         info.data[AUX_MOUSEX] = x;
  184.         info.data[AUX_MOUSEY] = y;
  185.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  186.         (*mouseDownTable[i].MouseFunc)(&info);
  187.         flag |= GL_TRUE;
  188.     }
  189.     }
  190.     return flag;
  191. }
  192.  
  193. static GLenum KeyDown(int key, GLenum status)
  194. {
  195.     GLenum flag;
  196.     int i;
  197.  
  198.     flag = GL_FALSE;
  199.     if (keyTableCount) {
  200.     for (i = 0; i < keyTableCount; i++) {
  201.         if (key == keyTable[i].keyField) {
  202.         (*keyTable[i].KeyFunc)();
  203.         flag |= GL_TRUE;
  204.         }
  205.     }
  206.     }
  207.     return flag;
  208. }
  209.  
  210. void auxExposeFunc(void (*Func)(int, int))
  211. {
  212.     tkExposeFunc(Func);
  213. }
  214.  
  215. void auxReshapeFunc(void (*Func)(int, int))
  216. {
  217.     tkExposeFunc(Func);
  218.     tkReshapeFunc(Func);
  219. }
  220.  
  221. void auxIdleFunc(void (*Func)(void))
  222. {
  223.     tkIdleFunc(Func);
  224. }
  225.  
  226. void auxKeyFunc(int key, void (*Func)(void))
  227. {
  228.     keyTable[keyTableCount].keyField = key;
  229.     keyTable[keyTableCount++].KeyFunc = Func;
  230. }
  231.  
  232. void auxMouseFunc(int mouse, int mode, void (*Func)(AUX_EVENTREC *))
  233. {
  234.     if (mode == AUX_MOUSEDOWN) {
  235.     mouseDownTable[mouseDownTableCount].mouseField = mouse;
  236.     mouseDownTable[mouseDownTableCount++].MouseFunc = Func;
  237.     } else if (mode == AUX_MOUSEUP) {
  238.     mouseUpTable[mouseUpTableCount].mouseField = mouse;
  239.     mouseUpTable[mouseUpTableCount++].MouseFunc = Func;
  240.     } else if (mode == AUX_MOUSELOC) {
  241.     mouseLocTable[mouseLocTableCount].mouseField = mouse;
  242.     mouseLocTable[mouseLocTableCount++].MouseFunc = Func;
  243.     } 
  244. }
  245.  
  246.  
  247. void auxDeleteMouseFunc( int mouse, int mode, void (*Func)(AUX_EVENTREC *))
  248. {
  249.    int i, j;
  250.  
  251.    for (i=0;i<mouseLocTableCount;i++) {
  252.       if (mouseLocTable[i].MouseFunc == Func) {
  253.          /* delete this one */
  254.          for (j=i+1;j<mouseLocTableCount;j++) {
  255.             mouseLocTable[j-1].MouseFunc = mouseLocTable[j].MouseFunc;
  256.             mouseLocTable[j-1].mouseField = mouseLocTable[j].mouseField;
  257.          }
  258.          mouseLocTableCount--;
  259.          break;
  260.       }
  261.    }
  262.  
  263. }
  264.  
  265.  
  266. static void idle(void)
  267. {
  268.    /* do nothing */
  269. }
  270.  
  271. void auxMainLoop(void (*Func)(void))
  272. {
  273.    if (animate) {
  274.       auxIdleFunc( idle );
  275.    }
  276.  
  277.     tkDisplayFunc(Func);
  278.     tkExec();
  279. }
  280.  
  281. void auxInitPosition(int x, int y, int width, int height)
  282. {
  283.     tkInitPosition(x, y, width, height);
  284. }
  285.  
  286. void auxInitDisplayMode(GLenum type)
  287. {
  288.     displayModeType = type;
  289.     tkInitDisplayMode(type);
  290. }
  291.  
  292. void auxInitDisplayModePolicy(GLenum type)
  293. {
  294.  
  295.     displayModePolicy = type;
  296. /*    tkInitDisplayModePolicy(type);*/
  297. }
  298.  
  299. GLenum auxInitDisplayModeID(GLint id)
  300. {
  301.    /* I don't know what this function's supposed to do.  It's just a stub. */
  302.     displayModeID = id;
  303. /*    tkInitDisplayModeID(id);*/
  304.     return (GLenum) id;
  305. }
  306.  
  307. GLenum auxInitWindow(char *title)
  308. {
  309.     int useDoubleAsSingle = 0;
  310.  
  311.     if (tkInitWindow(title) == GL_FALSE) {
  312.     if (AUX_WIND_IS_SINGLE(displayModeType)) {
  313.         tkInitDisplayMode(displayModeType|AUX_DOUBLE);
  314.         if (tkInitWindow(title) == GL_FALSE) {
  315.         return GL_FALSE;
  316.         }
  317.         fprintf(stderr, "Can't initialize a single buffer visual.\n");
  318.         fprintf(stderr, "Will use a double buffer visual instead,");
  319.         fprintf(stderr, "only drawing into the front buffer.\n");
  320.         displayModeType = displayModeType | AUX_DOUBLE;
  321.         useDoubleAsSingle = 1;
  322.     }
  323.     }
  324.     tkReshapeFunc(DefaultHandleReshape);
  325.     tkExposeFunc(DefaultHandleExpose);
  326.     tkMouseUpFunc(MouseUp);
  327.     tkMouseDownFunc(MouseDown);
  328.     tkMouseMoveFunc(MouseLoc);
  329.     tkKeyDownFunc(KeyDown);
  330.     auxKeyFunc(AUX_ESCAPE, auxQuit);
  331.     glClearColor(0.0, 0.0, 0.0, 1.0);
  332.     glClearIndex(0);
  333.     glLoadIdentity();
  334.     if (useDoubleAsSingle) {
  335.         glReadBuffer(GL_FRONT);
  336.     glDrawBuffer(GL_FRONT);
  337.     }
  338.     return GL_TRUE;
  339. }
  340.  
  341. void auxCloseWindow(void)
  342. {
  343.     tkCloseWindow();
  344.     keyTableCount = 0;
  345.     mouseDownTableCount = 0;
  346.     mouseUpTableCount = 0;
  347.     mouseLocTableCount = 0;
  348. }
  349.  
  350. void auxQuit(void)
  351. {
  352.     tkQuit();
  353. }
  354.  
  355. void auxSwapBuffers(void)
  356. {
  357.     tkSwapBuffers();
  358. }
  359.  
  360. #if !defined(AMIGA) && !defined(__WIN32__)
  361. /* for systems with X only... */
  362. Display *auxXDisplay(void)
  363. {
  364.     Display *ptr;
  365.     
  366.     tkGetSystem(TK_X_DISPLAY, (void *)&ptr);
  367.     return ptr;
  368. }
  369.  
  370. Window auxXWindow(void)
  371. {
  372.     Window ptr;
  373.     
  374.     tkGetSystem(TK_X_WINDOW, (void *)&ptr);
  375.     return ptr;
  376. }
  377. #endif
  378.  
  379. GLenum auxGetDisplayModePolicy(void)
  380. {
  381.    return displayModePolicy;
  382. /*    return tkGetDisplayModePolicy();*/
  383. }
  384.  
  385. GLint auxGetDisplayModeID(void)
  386. {
  387. /*    return tkGetDisplayModeID();*/
  388.    return displayModeID;
  389. }
  390.  
  391. GLenum auxGetDisplayMode(void)
  392. {
  393. /*    return tkGetDisplayMode();*/
  394.    return displayModeType;
  395. }
  396.  
  397. void auxSetOneColor(int index, float r, float g, float b)
  398. {
  399.     tkSetOneColor(index, r, g, b);
  400. }
  401.  
  402. void auxSetFogRamp(int density, int startIndex)
  403. {
  404.     tkSetFogRamp(density, startIndex);
  405. }
  406.  
  407. void auxSetGreyRamp(void)
  408. {
  409.     tkSetGreyRamp();
  410. }
  411.  
  412. void auxSetRGBMap(int size, float *rgb)
  413. {
  414.     tkSetRGBMap(size, rgb);
  415. }
  416.  
  417. int auxGetColorMapSize(void)
  418. {
  419.  
  420.     return tkGetColorMapSize();;
  421. }
  422.  
  423. void auxGetMouseLoc(int *x, int *y)
  424. {
  425.     tkGetMouseLoc(x, y);
  426. }
  427.  
  428. void auxGetScreenSize( GLint *width, GLint *height )
  429. {
  430.    /* This is a kludge! */
  431.    *width = 1280;
  432.    *height = 1024;
  433. }
  434.  
  435.  
  436. void auxAnimation( GLint state )
  437. {
  438.    animate = state;
  439. }
  440.  
  441.